package cn.xydpeng.springframework.core.io;

import cn.xydpeng.springframework.lang.Nullable;
import cn.xydpeng.springframework.util.Assert;
import cn.xydpeng.springframework.util.ClassUtils;

/**
 * @author xydpeng
 * @version v1.0
 * @date: 2022/10/14
 * @description:
 */
public class DefaultResourceLoader implements ResourceLoader {

    @Nullable
    private ClassLoader classLoader;

    public ClassLoader getClassLoader() {
        return this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader();
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "location must not be null");
        if (location.startsWith(CLASSPATH_URL_PREFIX)) {
            return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
        } else if (location.startsWith("/")) {
            //TODO
        }
        return null;
    }
}
